package VBox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;
import java.util.logging.Logger;


/**
 * A class for triggering commands in a separate thread. For testing purpose only.
 * @author k4rthikR
 *
 */
public class VMTask implements Callable<Integer> {
	public String cmd=new String("");
	 
	Logger log = Logger.getAnonymousLogger();
	public Integer call() {
		log.info("VMTask Commad run : "+cmd);
		Runtime rt = Runtime.getRuntime();
		int exitVal=-1;
		try {
			Process pr = rt.exec(cmd);
			BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			 
            String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            exitVal = pr.waitFor();
		} catch (IOException e) {
			log.severe(e.getMessage());
			//e.printStackTrace();
		} catch (InterruptedException e) {
			//Auto-generated catch block
			//e.printStackTrace();
			log.severe(e.getMessage());
		}
		log.info("\tCompleted");
		return exitVal;	
	}
	public void run() {
		log.info("VMTask Commad run : "+cmd);
		Runtime rt = Runtime.getRuntime();
		int exitVal=-1;
		try {
			Process pr = rt.exec(cmd);
			BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			 
            String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            exitVal = pr.waitFor();
		} catch (IOException e) {
			log.severe(e.getMessage());
			//e.printStackTrace();
		} catch (InterruptedException e) {
			//Auto-generated catch block
			//e.printStackTrace();
			log.severe(e.getMessage());
		}
		log.info("\tCompleted with exitval "+exitVal);
		
		
	}
	
	
}